var a = false, b = false, c = false, d = false, e = false;

if (typeof g === undefined) {
    var g = {};
}
g.checkUser = function (name) {
    var nameRegex = /^(?!_)(?!.*?_$)[a-zA-Z0-9]+$/;
    var len = lib.data.getStrLength(name);
    if (name == 0) {
        $("tipUsername").innerHTML = "<em style='color:red;'>"+lan["04"]+"</em>";
        a = false;
    }
    else if (!nameRegex.test(name)) {
        $("tipUsername").innerHTML = "<em style='color:red;'>"+lan["05"]+"</em>";
        a = false;
    }
    else if (len < 4 || len > 15) {
        $("tipUsername").innerHTML = "<em style='color:red;'>"+lan["06"]+"</em>";
        a = false;
    }
    else {
        a = true;
    }
    g.verifyUsername(name);
};

g.checkPassw = function(pass) {
    var allNumRegex = /^\d+$/.test(pass);
    var haveNumRegex = /\d/.test(pass);
    var allhaveAbcRegex = /^[a-zA-Z]+$/.test(pass);
    var haveAbcRegex = /[a-zA-Z]/.test(pass);
    var haveStrongRegex = /[^a-zA-Z0-9]/.test(pass);
    var veryStrongRegex = pass.split(/[^a-zA-Z_0-9]/).length > 2;
    var len = lib.data.getStrLength(pass);
    if (len == 0) {
        $("tipPassword").innerHTML = "<em style='color:red;'>"+lan["07"]+"</em>";
        b = false;
    }
    else if (len < 4 || len > 20) {
        $("tipPassword").innerHTML =  "<em style='color:red;'>"+lan["08"]+"</em>";
        b = false;
    }
    else if ((allNumRegex || allhaveAbcRegex) || (haveNumRegex && haveAbcRegex && len < 8)) {
        $("tipPassword").innerHTML =  "<em style='color:red;'>"+lan["09"]+"</em>";
        b = true;
    }
    else {
        $("tipPassword").innerHTML = "";
        b = true;
    }
    if ((haveNumRegex && haveAbcRegex && len >= 8) || (haveNumRegex && haveAbcRegex && haveStrongRegex && len < 8) || ((haveNumRegex && haveStrongRegex) || (haveAbcRegex && haveStrongRegex))) {
        $("tipPassword").innerHTML = "<em style='color:#ff9900;'>"+lan["10"]+"</em>";
    }
    if ((haveNumRegex && haveAbcRegex && haveStrongRegex && len >= 8) || (haveNumRegex && haveAbcRegex && veryStrongRegex && len < 8) || (((haveNumRegex && haveStrongRegex) || (haveAbcRegex && haveStrongRegex)) && len >= 8)) {
        $("tipPassword").innerHTML = "<em style='color:#00cc00;'>"+lan["11"];
    }
}

g.checkPassw2 = function (rep) {
    var passWord = $("regPassword").value;
    if (rep == 0) {
        $("tipRepassword").innerHTML ="<em style='color:red;'>"+lan["12"]+"</em>";;
        c = false;
    }
    else if (rep != passWord) {
        $("tipRepassword").innerHTML ="<em style='color:red;'>"+lan["13"]+"</em>";
        c = false;
    }
    else {
        $("tipRepassword").innerHTML = "";
        c = true;
    }
}

g.checkMail = function(mail) {
    var mailRegex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
    if (mail == 0) {
        $("tipMail").innerHTML = "<em style='color:red;'>"+lan["14"]+"</em>";
        d = false;
    } else if (!mailRegex.test(mail)) {
        $("tipMail").innerHTML ="<em style='color:red;'>"+lan["15"]+"</em>";
        d = false;
    } else {
        $("tipMail").innerHTML = "";
        d = true;
    }
}

g.responseRegister = function (resTxt) {
    var tN = $("regUsername").value;
    g.tip.clear();
    if (resTxt == "3") {//"3"表示成功
        lib.cookie.set("username", tN, 30, "/", true);
        lib.cookie.set("lastUsername", tN, 300, "/", true);
        g.user.setUserInfo();
        $("tabContent2").innerHTML = lan["16"];
        lib.dom.setStyle($("tabContent2"), "color:red;");
        setTimeout(g.lightOn.cancle, 3000);
        setTimeout(function () {
            window.location.href = "http:\/\/" + tN + /\w+\.\w+$/.exec(window.location.host)[0]; //中转到新的url
        }, 4000);
    }
    else {//失败
        var t = document.createTextNode(lan["17"]);
        $("tabContent2").appendChild(t);
        lib.dom.setStyle($("tabContent2"), "color:red;");
        setTimeout(g.lightOn.cancle, 3000);
    }
};

g.registerUser = function () {
    if (a && b && c && d) {
        var second;
        var url;
        var returnValue = "2";
        var data = "{'action':'1','data':{'username':'" + $("regUsername").value + "','email':'" + $("regEmail").value + "','password':'" + $("regPassword").value+"'}}";
        postAndGetData(true, g.domainDirectory + "/register.aspx", data, g.responseRegister);
        g.tip.display(lan["03"]);
        if (returnValue == "3") {//successed
        }
    }
}

g.verifyUsername = function (username) {
    var data = "{'action':'0','data':{'username':'" + $("regUsername").value + "'}}";
    postAndGetData(true, g.domainDirectory + "/register.aspx", data, g.resVerifyUsername);
}

g.resVerifyUsername = function (resTxt) {
    if (resTxt == "2") {
        d = false;
        var t = document.createTextNode(lan["18"]);
        $("tabContent1").appendChild(t);
    } else if (resTxt == "2") {
        var t = document.createTextNode(lan["19"]);
        $("tabContent1").appendChild(t);
    } else {
    }
    
}

g.logIn = function () {
    g.tip.display(lan["03"]);
    var url = g.URI.protocol + '://' + g.URI.authority + "/login.aspx?act=login&&currentUser=0&&username=" + escape($("UsernameID").value) + "&&password=" + escape($("PasswordID").value) + "&&f=" + (new Date).getTime();
    postAndGetData(true, url, "", g.responseForLogIn);
}

g.wait = function () {
    var t = $("tipLogin");
    if (t == undefined) return;
    if (t.innerHTML == "") {
        t.innerHTML = "10";
    }else if (parseInt(t.innerHTML) > -1) {
        t.innerHTML = parseInt(t.innerHTML);
    } else {
        t.innerHTML = "";
    }
}

g.responseForLogIn = function (ResTxt) {
    var tmp = lib.data.getValueFromContent(ResTxt, "result");
    if ("nouser" == tmp) {
        var tmpB = $("tipLogin");
        tmpB.innerHTML =lan["20"];
    }
    else {
        g.user = new User(lib.data.getValueFromContent(ResTxt, "username"), "")
        hideTab($('light'));
        minimize($('fade'));
        lib.cookie.set("lastUsername", g.user.username, 300, "/", true);
        lib.cookie.set("checkCode", lib.data.getValueFromContent(ResTxt, "checkcode"), 300, "/", true);
        g.user.setUserInfo();
        g.lightOn.cancle();
    }
}

window.g = g;

